﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SksApp
{
    public enum Information : byte
    {
        Sunucu,
        Veritabani,
        VtKullaniciAdi,
        VtSifre,
        MagazaNo,
        KasaNo,
        SrmMerkezKodu,
        VeritabaniYolu,
        FisYazicisi,
        FaturaYazicisi,
        EtiketYazicisi,
        RaporYazicisi,
        IadeYazicisi,
        TeraziTipi,
        TeraziPortNo,
        KasaTipi,
        KasaPortNo,
        DetayliSatisGonderilsin,
        YazdirmaSorulsun,
        TeraziDosyaYolu,
        TeraziProgrami,
        SonBilgiAlimTarihi,
        CekmeceProgrami,
        CekmeceAcilsin,
        YazdirmaModu,
        DefaultPrice,
        MusteriGostergePortu,
        KurusHanesi,
        EvrakSeri
    }

    public class Settings
    {
        public bool DemoKullanim { get; set; }

        private string _path;

        public string Path
        {
            get { return _path; }

            set
            {
                _path = value;
                _path = string.Format("{0}\\Ayarlar.ini", _path);
            }
        }

        private static Settings _instance;

        public Settings()
        {
            allValue = new Dictionary<Information, string>();
        }

        public static Settings Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Settings();
                return _instance;
            }
        }

        private Dictionary<Information, string> allValue = null;

        public void AddItems(Information item, string value)
        {
            if (_instance.allValue.ContainsKey(item))
                _instance.allValue[item] = value;
            else
                _instance.allValue.Add(item, value);
        }

        public string GetValue(Information info)
        {
            ItemControl();
            string result = string.Empty;
            if (_instance.allValue.ContainsKey(info))
                result = _instance.allValue[info];
            return result;
        }

        public void RemoveItem(Information info)
        {
            ItemControl();
            if (_instance.allValue.ContainsKey(info))
                _instance.allValue.Remove(info);
        }

        public void Save()
        {
            using (StreamWriter writer = new StreamWriter(_instance.Path))
            {
                foreach (KeyValuePair<Information, string> item in _instance.allValue)
                {
                    writer.WriteLine(string.Format("{0};{1}", item.Key, item.Value));
                }
            }
        }

        public void Update(Information info, string value)
        {
            ItemControl();
            if (_instance.allValue.ContainsKey(info))
                _instance.allValue[info] = value;
        }

        public void Load()
        {
            if (File.Exists(_instance.Path))
            {
                _instance.allValue.Clear();
                using (StreamReader reader = new StreamReader(_instance.Path))
                {
                    while (reader.Peek() != -1)
                    {
                        string[] readValue = reader.ReadLine().Split(';');
                        if (Enum.GetNames(typeof(Information)).Where(x => x.Equals(readValue[0])).Count() > 0)
                        {
                            _instance.AddItems((Information)Enum.Parse(typeof(Information), readValue[0]), readValue[1]);
                        }
                    }
                }
            }
        }

        private void ItemControl()
        {
            if (allValue != null && allValue.Count <= 0)
            {
                Load();
            }
        }

        public bool LisansControl(int programID)
        {
            Stream str = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("TouchApp.Resources.LicenseTools.dll");
            byte[] data = new BinaryReader(str).ReadBytes((int)str.Length);
            Assembly testAssembly = Assembly.Load(data);
            Type calcType = testAssembly.GetType("LicenseTools.LicenseMgr");
            object calcInstance = Activator.CreateInstance(calcType, new object[] { programID });
            bool[] sonuc = (bool[])calcType.InvokeMember("Check", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, null);
            this.DemoKullanim = sonuc[1];
            return sonuc[0];
        }
    }
}
